package org.simple.framework.web.result.base;

import lombok.Data;
import org.simple.framework.web.result.model.BaseCode;

/**
 * 项目名称：simple-web-result
 * 类名称：BaseResult
 * 类描述：BaseResult 统一返回结果类
 * 创建时间：2018/1/16 17:06
 *
 * @author sisyphus   (E-mail:1620657419@qq.com)
 * @version v1.0
 */
@Data
public class SimpleResult<T> {

    /**
     * 返回状态码
     *
     * @see BaseCode
     */
    private Integer code;

    /**
     * 返回信息
     */
    private String message;

    /**
     * 返回数据
     */
    private T data;

    /**
     * 是否成功
     */
    private Boolean isSuccess;

    private ErrorInfo error = ErrorInfo.EMPTY;

    public SimpleResult(BaseCode baseCode) {
        this.setBaseCode(baseCode);
    }

    public SimpleResult(BaseCode baseCode, T data) {
        this(baseCode);
        this.data = data;
    }

    public static SimpleResult ok() {
        return new SimpleResult(SimpleResultCode.SUCCESS);
    }

    public static <T> SimpleResult<T> ok(T t) {
        return new SimpleResult(SimpleResultCode.SUCCESS, t);
    }

    public void setBaseCode(BaseCode baseCode) {
        this.code = baseCode.getCode();
        this.message = baseCode.getMessage();
        this.isSuccess = baseCode.getIsSuccess();
    }

    public void buildError(Exception e) {
        ErrorInfo error = new ErrorInfo();
        error.setMessage(e.getMessage());
        error.setStackTrace(e.getStackTrace());
        this.error = error;
    }


}